
// gcc hetero_check.c re.c -o hetero_check

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "re.h"

// 判断是否至少包含两种架构、且每种架构 >= 2 台
int hetero_check(const char *text)
{
    const char *arches[] = {"x86", "arm", "riscv", "ppc", "mips"};
    int arch_count[5] = {0};
    int arch_total = sizeof(arches) / sizeof(arches[0]);

    for (int i = 0;  i < arch_total;  i++) {
        re_t pattern = re_compile(arches[i]);
        const char *p = text;
        int match_len = 0;
        int pos;

        while ((pos = re_matchp(pattern, p, &match_len)) != -1) {
            arch_count[i]++;
            p += pos + match_len; // 移动指针
        }
        //re_free(pattern);
    }

    int kinds = 0;
    for (int i = 0; i < arch_total; i++) {
        if (arch_count[i] >= 2)
            kinds++;
    }

    return kinds >= 2;
}

int main() {
    const char *cluster1 = "node1_x86 node2_x86 node3_arm node4_arm";
    const char *cluster2 = "node1_x86 node2_x86 node3_x86 node4_x86";

    printf("cluster1 hetero: %d\n", hetero_check(cluster1));
    printf("cluster2 hetero: %d\n", hetero_check(cluster2));

    return 0;
}